package com.wx.spider.common.model;

import com.alibaba.fastjson.JSON;

import java.io.Serializable;

/**
 * @Description: 返回的通用实体类
 * @Author:	qingfeng.shen
 * @CreateDate:	2019年1月2日
 */
public class CommonResponse<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    public static final String SUCCESS = "0000";
    public static final String SUCCESS_MSG = "操作成功";

    //频率限制
    public static final String OP_IN_PROGRESS = "0001";
    public static final String OP_IN_PROGRESS_MSG = "请勿频繁操作";


    // 身份校验错误
    public static final String NO_LOGIN_ERROR = "0401";
    public static final String NO_LOGIN_ERROR_MSG = "身份校验未通过";
    
    
    // 参数错误
    public static final String PARAM_ERROR = "0400";
    public static final String PARAM_ERROR_MSG = "参数不正确";


    // 服务器内部错误
    public static final String INTERNAL_ERROR = "0500";
    public static final String INTERNAL_ERROR_MSG = "服务器正在开小差，请稍后重试";

    private String code;

    private String msg;

    private T data;


    public static final CommonResponse SUCCESS_RESPONSE = new CommonResponse(SUCCESS, SUCCESS_MSG);

    public static final CommonResponse OP_IN_PROGRESS_RESPONSE = new CommonResponse(OP_IN_PROGRESS, OP_IN_PROGRESS_MSG);

    public static final CommonResponse NO_LOGIN_ERROR_RESPONSE = new CommonResponse(NO_LOGIN_ERROR, NO_LOGIN_ERROR_MSG);
    
    public static final CommonResponse PARAM_ERROR_RESPONSE = new CommonResponse(PARAM_ERROR, PARAM_ERROR_MSG);

    public static final CommonResponse INTERNAL_ERROR_RESPONSE = new CommonResponse(INTERNAL_ERROR, INTERNAL_ERROR_MSG);


    public CommonResponse() {
    }

    public CommonResponse(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public CommonResponse(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> CommonResponse<T> succeed(String code, String msg, T data) {
        return new CommonResponse<T>(code, msg, data);
    }

    public static <T> CommonResponse<T> succeed(T model) {
        return succeed(SUCCESS, SUCCESS_MSG, model);
    }



    public static CommonResponse<String> getSuccessBean(String msg) {
        CommonResponse responseBean = new CommonResponse();
        responseBean.setCode(SUCCESS);
        responseBean.setMsg(msg);
        return responseBean;
    }

    public static CommonResponse<String> getOpInProgressBean(String msg) {
        CommonResponse responseBean = new CommonResponse();
        responseBean.setCode(OP_IN_PROGRESS);
        responseBean.setMsg(msg);
        return responseBean;
    }

    public CommonResponse(String code, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public String getCode() {
        return this.code;
    }

    public T getData() {
        return this.data;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

}
